using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;

using wnet.admin.Models;

using System;
using wnet.admin.wentcommon;
using Models;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;


namespace wnet.admin.Controllers.ywBusiness
{
    /// <summary>
    /// 钢丝绳信息
    /// </summary>
    [TokenAuth]
    [Route("business/ywwirerope/{action}")]
    public class ywwireropeController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public ywwireropeController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }


        /// <summary>
        /// 钢丝绳信息-列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost]
        public object list(yw_equipmentQueryDto parm)
        {

            var predicate = Expressionable.Create<yw_wirerope>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.typename), it => it.typename.Contains(parm.typename));
            if (parm.states == 3)
            {
                var startTime = DateTime.Now.Date.AddDays(-30); // 获取30天前当天的开始时间
                predicate = predicate.And(it => it.states == 1 && it.jianxiutime > startTime);
            }
            else
            {
                predicate = predicate.AndIF(parm.states != null && parm.states != 0, it => it.states == parm.states);
            }


            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.title), it => it.title.Contains(parm.title));
            var query = _sqlSugarClient.Queryable<yw_wirerope>()
                .Where(predicate.ToExpression())
                .Select((u) => new yw_wirerope
                {
                    id = u.id.SelectAll(),

                });

            var datalist = query.ToPage(parm);

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 钢丝绳信息-详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Getyw_equipment(int id)
        {
            var response = _sqlSugarClient.Queryable<yw_wirerope>()
                .Where(q => q.id == id).First();

            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 钢丝绳信息-添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Addyw_equipment([FromBody] yw_wirerope parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Insertable<yw_wirerope>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 钢丝绳信息-编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Updateyw_equipment([FromBody] yw_wirerope parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Updateable<yw_wirerope>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 钢丝绳信息-删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Deleteyw_equipment(int id)
        {
            try
            {
                var response = _sqlSugarClient.Queryable<yw_wirerope>()
            .Where(q => q.id == id).First();
                if (response == null)
                {
                    throw new Exception("数据不存在");
                }
                _sqlSugarClient.Deleteable<yw_wirerope>(response).ExecuteCommand();
                var data = new { code = 200, msg = "成功", data = response };

                return Json(data, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
            }
            catch (Exception ex)
            {

                var data = new { code = -1, msg = ex.Message, data = "null" };

                return Json(data, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
            }
        }
    }
}